package com.chenxiao.Composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CX
 * @Date: Created in  2024/04/03 10:13
 * @version: v1.0
 * @Description: TODO 组合模式中的容器节点
 **/
public class Composite implements Component{
    private List<Component> components = new ArrayList<>();

    public void add(Component component){
        components.add(component);
    }
    public void remove(Component component){
        components.remove(component);
    }

    @Override
    public void operation() {
        // 容器节点的具体操作
        System.out.println("Composite operation.");
        // 递归调用所有子组件的operation方法
        for (Component component : components) {
            component.operation();
        }
    }
}
